package com.gitee.flyzing.jxt.component;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yaoguangyao
 * @Date 2021年6月29日
 */
public class JXTRow {
	private Map<Integer, JXTCell> cells = new HashMap<>();
	private transient boolean isShow = true;
	public static JXTRow createJXTRow(Row row, Object datas) {
		return createJXTRow(row, datas, null);
	}


	public static JXTRow createJXTRow(Row row, Object datas, Map params) {
		JXTRow jxtRow = new JXTRow();
		int minColIx = row.getFirstCellNum();
		int maxColIx = row.getLastCellNum();
		for (int colIx = minColIx; colIx < maxColIx; colIx++) {
			Cell cell = row.getCell(colIx);
			if (cell == null) {
				continue;
			}
			JXTCell jxtCell = JXTCell.createJXTCell(cell, datas, params);
			if (jxtCell instanceof JXTEachCell) {
				return JXTEachRow.createJXTEachRow((JXTEachCell) jxtCell);
			} else {
				jxtRow.add(colIx, jxtCell);
			}
		}
		return jxtRow;
	}

	public void add(int col, JXTCell jxtCell) {
		cells.put(col, jxtCell);
	}

	public Map<Integer, JXTCell> getCells() {
		return cells;
	}

	public void setCells(Map<Integer, JXTCell> cells) {
		this.cells = cells;
	}

	public void createXSSFRow(XSSFSheet xssfSheet, int row) {
		XSSFRow xssfRow = xssfSheet.createRow(row);
		for (Map.Entry<Integer, JXTCell> entry : cells.entrySet()) {
			int col = entry.getKey();
			JXTCell jxtCell = entry.getValue();
			if (jxtCell != null) {
				jxtCell.createXSSFCell(xssfRow, row, col);
			}
		}
	}

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean show) {
		isShow = show;
	}
}
